package com.example.status;

import com.example.model.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.state.BroadcastState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.common.state.ReadOnlyBroadcastState;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.streaming.api.datastream.BroadcastConnectedStream;
import org.apache.flink.streaming.api.datastream.BroadcastStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.BroadcastProcessFunction;
import org.apache.flink.util.Collector;

/**
 * Created with IntelliJ IDEA.
 * ClassName: OperatorBroadcastStateDemo
 * Package: com.example.status
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-07-27
 * Time: 20:12
 */

//广播状态
public class OperatorBroadcastStateDemo {
    public static void main(String[] args) throws Exception {

        //水位超过指定的阈值发送告警，阈值可以动态修改。
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //数据流
        SingleOutputStreamOperator<WaterSensor> data = env.socketTextStream("hadoop102", 7777)
                .map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        final String[] s = value.split(" ");

                        return new WaterSensor(s[0], Long.valueOf(s[1]), Integer.valueOf(s[2]));
                    }
                });

        //1.创建广播流 广播配置
        DataStreamSource<String> ds = env.socketTextStream("hadoop102", 8888);

        //将 配置流 广播

        MapStateDescriptor<String, Integer> state = new MapStateDescriptor<>("state", Types.STRING, Types.INT);
        //得到一条广播流
        BroadcastStream<String> broadcast = ds.broadcast(state);

        //2.把数据流 和 广播后的配置流 合流算子
        //生成广播的连接流
        BroadcastConnectedStream<WaterSensor, String> connect = data.connect(broadcast);

        //3.调用process
        connect.process(
                new BroadcastProcessFunction<WaterSensor, String, String>() {
                    //处理数据流
                    @Override
                    public void processElement(WaterSensor value, ReadOnlyContext ctx, Collector<String> out) throws Exception {
                        //5.通过上下文获取广播状态 取出里面的值 (只读，不能修改)
                        ReadOnlyBroadcastState<String, Integer> broadcastState = ctx.getBroadcastState(state);
                        //同样是获取状态 但是返回的数据流不一样 只读广播状态
                        Integer threshold = broadcastState.get("threshold");
                        //刚启动 可能数据流的第一条数据先来
                        threshold = threshold == null ? 0 : threshold;
                        if (value.getVc() > threshold) {
                            out.collect("水位超过指定的阈值: " + threshold + "!!!");
                        }
                    }

                    //处理广播后配置流的处理方法 广播流才能修改
                    @Override
                    public void processBroadcastElement(String value, Context ctx, Collector<String> out) throws Exception {
                        //4.通过上下文获取广播状态 参数是状态表述其 就是上面的定义的
                        BroadcastState<String, Integer> broadcastState = ctx.getBroadcastState(state);
                        //就是一个Map
                        broadcastState.put("threshold", Integer.valueOf(value));
                    }
                }
        ).print();

        env.execute();


    }
}
